package com.lock8;

import java.util.concurrent.TimeUnit;

/**
 * 对于静态方法，锁的是Class，是一个模板
 */
public class Test3 {
    public static void main(String[] args) {

        Phone3 phone1 = new Phone3();
        Phone3 phone2 = new Phone3();

        new Thread(() -> {
            try {
                phone1.sendMsg();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "A").start();

//        try {
//            TimeUnit.SECONDS.sleep(1);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        new Thread(() -> {
            phone2.call();
        }, "B").start();
    }

}

class Phone3 {

    //synchronized锁的对象是方法的调用者

    public static synchronized void sendMsg() throws InterruptedException {
        TimeUnit.SECONDS.sleep(4);
        System.out.println("发送短信");
    }

    public static synchronized void call() {
        System.out.println("打电话");
    }

    public void hello(){
        System.out.println("hello");
    }

}

